﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using MP3Ganizer.Modules.Visualisation.ViewModels;
using MP3Ganizer.Common;
using MP3Ganizer.Modules.Visualisation.Views;

namespace MP3Ganizer.Modules.Visualisation.Controllers
{
    /// <summary>
    /// Controller du Visualisation
    /// </summary>
    public class VisualisationController : IVisualisationController
    {
        #region Fields
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private readonly IVisualisationVM _visualisationVM;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructeur. Les paramètres sont pré-remplis par le UnityContainer.
        /// </summary>
        /// <param name="container"></param>
        /// <param name="regionManager"></param>
        /// <param name="FileViewMainVM"></param>
        public VisualisationController(IUnityContainer container, IRegionManager regionManager, IVisualisationVM visualisationVM)
        {
            this._container = container;
            this._regionManager = regionManager;
            this._visualisationVM = visualisationVM;
            this._container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Subscribe(OnUserAction, Microsoft.Practices.Composite.Presentation.Events.ThreadOption.UIThread, true);
        }

        private void OnUserAction(MP3Ganizer.Events.User.UserActionEventArgs e)
        {
            if (e.Action == MP3Ganizer.Common.Enums.UserAction.ShowMedia)
            {
                if (e.Data is Events.User.ShowMediaData)
                {

                    bool isRegionPresent = this._regionManager.Regions.ContainsRegionWithName(RegionNames.Visualisation);
                    if (!isRegionPresent)
                    {
                        if(e.CallBackMethod != null)
                            e.CallBackMethod();
                        return;
                    }
                    List<object> views = this._regionManager.Regions[RegionNames.Visualisation].Views.ToList<object>();
                    views.ForEach(v => this._regionManager.Regions[RegionNames.Visualisation].Deactivate(v));
                    Events.User.ShowMediaData data = e.Data as Events.User.ShowMediaData;
                    views.ForEach(v =>
                    {
                        if (data.DataType == Common.Enums.DataType.Audio)
                        {
                            if (v is IVisualisationAudioV)
                            {
                                this._regionManager.Regions[RegionNames.Visualisation].Activate(v);
                                IVisualisationAudioV vAudio = v as IVisualisationAudioV;
                                vAudio.Pause();
                            }
                        }
                        if (data.DataType == Common.Enums.DataType.Image)
                        {

                            if (v is IVisualisationImageV)
                            {
                                this._regionManager.Regions[RegionNames.Visualisation].Activate(v);
                            }
                                

                        }
                        if (data.DataType == Common.Enums.DataType.Unknown)
                        {

                            if (v is IVisualisationDefaultV)
                                this._regionManager.Regions[RegionNames.Visualisation].Activate(v);
                            if (v is IVisualisationImageV)
                            {
                                IVisualisationImageV vImage = v as IVisualisationImageV;
                                vImage.DisposeResources();
                            }
                            if (v is IVisualisationAudioV)
                            {
                                IVisualisationAudioV vAudio = v as IVisualisationAudioV;
                                vAudio.DisposeResources();
                            }
                        }
                    });
                    _visualisationVM.OnControllerChanged(e);
                }
            }
        }
        #endregion

        #region IVisualisationController Members

        /// <summary>
        /// Lance le Controller: Injecte la vue dans la région
        /// </summary>
        public void Run()
        {
            this._regionManager.RegisterViewWithRegion(RegionNames.Visualisation,
                                              () => this._visualisationVM.DefaultView);
            ;
            this._regionManager.RegisterViewWithRegion(RegionNames.Visualisation,
                                              () => this._visualisationVM.AudioView);
            ;
            this._regionManager.RegisterViewWithRegion(RegionNames.Visualisation,
                                              () => this._visualisationVM.ImageView);
            ;
            
        }
        #endregion
    }
}
